package org.social.manager;

import java.util.List;
import java.util.Map;

import org.constant.MailConstant;
import org.error.GMErrorCode;
import org.social.db.bean.PlayerMap;
import org.social.db.cache.PlayerMapCache;
import org.utils.RemoteUtil;

import com.dal.gm_api.util.GMError;
import com.xiaoleilu.hutool.util.RandomUtil;

/**
 * Created by fxf on 2018-1-11.
 */
public class GMMailManager {

    /**
     * 发送邮件
     *
     * @param type         邮件类型{@link MailConstant}
     * @param title        邮件标题
     * @param body         邮件正文
     * @param item         邮件附件（道具）
     * @param bodyParam    正文参数
     * @param playerIdList 玩家idList
     */
    public static void sendMail(int type, String title, String body, Map<Integer, Integer> item, Map bodyParam, List<Integer> playerIdList) {
        //2.发送邮件
//        mailList.forEach(mail -> {
//            if (mail.getType() == MailConstant.MAIL_TYPE_PERSONAL) {
//                RemoteUtil.gameAsyn(mail.getServerId()).sendGMMail(mail);
//            } else {
		    	if (type == MailConstant.MAIL_TYPE_PERSONAL) {
		    		playerIdList.forEach(pid -> {
		    			PlayerMap pm = PlayerMapCache.me().getByPid(pid);
		    			GMError.isAndTrue(GMErrorCode.NOT_FOND_TARGET, pm == null);
		    		});
				}
                RemoteUtil.gameTopic().sendGMMail(RandomUtil.randomUUID(),type, title, body, item, bodyParam, playerIdList);
//            }
//        });
    }

    public static List<String> queryMail(PlayerMap playerMap, long startTime, long endTime) {
        return RemoteUtil.gameSync(playerMap.getServerId()).queryGMMail(playerMap.getPid(), startTime, endTime);
    }

//    private static List<GMMailDTO> buildGMMailDTO(int type, String title, String body, Map<Integer, Integer> items, Map bodyParam, List<Integer> playerIdList) {
//        GMMailDTO dto = new GMMailDTO();
//        dto.setType(type)
//                .setBodyParam(bodyParam)
//                .setItems(items)
//                .setTitle(title)
//                .setBody(body);
//        return buildPersonalMail(dto, playerIdList);
//    }
//
//    private static List<GMMailDTO> buildPersonalMail(GMMailDTO dto, List<Integer> playerIdList) {
//        List<GMMailDTO> result = Lists.newArrayList();
//        Map<String, GMMailDTO> mailMap = Maps.newHashMap();
//        if (dto.getType() == MailConstant.MAIL_TYPE_SERVER) {
//            dto.setUuid(UUID.randomUUID().toString());
//            result.add(dto);
//        } else {
//            playerIdList.forEach(pid -> {
//            	PlayerMap playerMap = PlayerMapCache.me().getByPid(pid);
//                if (playerMap == null)
//                    return;
//                GMMailDTO mail = mailMap.computeIfAbsent(playerMap.getServerId(), k -> dto.copy());
//                mail.getPlayerIdList().add(pid);
//                mail.setServerId(playerMap.getServerId());
//            });
//            result.addAll(mailMap.values());
//        }
//        return result;
//    }
}
